//
//  Wallet_RechargeViewController.m
//  fxxiaoke
//
//  Created by 鞠超 on 2016/10/13.
//  Copyright © 2016年 jc. All rights reserved.
//

#import "Wallet_RechargeViewController.h"

#import "Add_RechargeCardViewController.h"

@interface Wallet_RechargeViewController () <UIScrollViewDelegate, UITextFieldDelegate>

@property (nonatomic, strong) UIScrollView * scrollView;
//
@property (nonatomic, strong) UIImageView * image_Pic;
@property (nonatomic, strong) UILabel * name;
@property (nonatomic, strong) UILabel * detail;
@property (nonatomic, strong) UIImageView * image_return;

//
@property (nonatomic, strong) UITextField * text_Money;

//
@property (nonatomic, strong) UIButton * btn_Next;

@end

@implementation Wallet_RechargeViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    [userdefault setObject:@"0" forKey:@"Wallet_Recharge"];

    [self p_navi];
    
    [self p_setupView];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - navi
- (void )p_navi
{
    _btnLeft.hidden = YES;
    _lblLeft.hidden = YES;
    _btnRight.hidden = YES;
    _lblRight.hidden = YES;
    
    _lblTitle.text = @"零钱充值";
    _topView.backgroundColor = navi_bar_bg_color;
    
    //左返回
    UIImageView * image_LeftRetrun = [[UIImageView alloc] initWithFrame:CGRectMake(11, 31.5, 25, 25)];
    image_LeftRetrun.userInteractionEnabled = YES;
    image_LeftRetrun.image = [UIImage imageNamed:@"Left-return.png"];
    [_topView addSubview:image_LeftRetrun];
    
    UILabel * lbl_return = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(image_LeftRetrun.frame) - 6, 35, 35, 15)];
    lbl_return.text = @"返回";
    lbl_return.textColor = [UIColor whiteColor];
    lbl_return.font = [UIFont systemFontOfSize:17];
    [_topView addSubview:lbl_return];
    
    UIButton * btn_return = [UIButton buttonWithType:(UIButtonTypeSystem)];
    btn_return.frame = CGRectMake(10, 22, 60, 40);
    [_topView addSubview:btn_return];
    [btn_return addTarget:self action:@selector(btn_returnAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

//左返回
- (void )btn_returnAction:(UIButton *)sender
{
    [self.navigationController popViewControllerAnimated:YES];
}

//显示tabbar
- (void )viewWillAppear:(BOOL)animated
{
    NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    
    if(![[userdefault objectForKey:@"Wallet_Recharge"] isEqualToString:@"0"])
    {
        [self btn_viewWhiteAction:nil];
    }
    
    [(AppDelegate *)[[UIApplication sharedApplication] delegate] hiddenTabBar];
}

#pragma mark - 布局
- (void )p_setupView
{
    self.view.backgroundColor = [UIColor groupTableViewBackgroundColor];
    
    self.scrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT - 64)];
    self.scrollView.backgroundColor = [UIColor groupTableViewBackgroundColor];
    self.scrollView.delegate = self;
    self.scrollView.showsVerticalScrollIndicator = NO;
    self.scrollView.contentSize = CGSizeMake(0, SCREEN_HEIGHT - 60);
    [self.view addSubview:self.scrollView];
    
    //
    UIView * view_White = [[UIView alloc] initWithFrame:CGRectMake(0, 15, SCREEN_WIDTH, 80)];
    view_White.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_White];
    
    self.image_Pic = [[UIImageView alloc] initWithFrame:CGRectMake(20, 20, 40, 40)];
    self.image_Pic.image = [UIImage imageNamed:@"Wchat.png"];
    self.image_Pic.layer.masksToBounds = YES;
    [view_White addSubview:self.image_Pic];
    
    //
    self.name = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.image_Pic.frame) + 20, 17, SCREEN_WIDTH - CGRectGetMaxX(self.image_Pic.frame) - 20 - 60, 20)];
    self.name.text = @"微信支付";
    self.name.font = [UIFont systemFontOfSize:18.0f];
    [view_White addSubview:self.name];
 
    //
    self.detail = [[UILabel alloc] initWithFrame:CGRectMake(CGRectGetMaxX(self.image_Pic.frame) + 20, CGRectGetMaxY(self.name.frame) + 8, SCREEN_WIDTH - CGRectGetMaxX(self.image_Pic.frame) - 20 - 60, 15)];
    self.detail.text = @"使用微信安全支付";
    self.detail.font = [UIFont systemFontOfSize:15.0f];
    self.detail.textColor = [UIColor grayColor];
    [view_White addSubview:self.detail];
    
    self.image_return = [[UIImageView alloc] init];
    self.image_return.frame = CGRectMake(SCREEN_WIDTH - 10 - 20, 30, 20, 20);
    self.image_return.image = [UIImage imageNamed:@"rigth-return.png"];
    [view_White addSubview:self.image_return];
    
    //点击换充值方式
    UIButton * btn_viewWhite = [UIButton buttonWithType:(UIButtonTypeCustom)];
    btn_viewWhite.frame = CGRectMake(0, 0, SCREEN_WIDTH, 80);
    [view_White addSubview:btn_viewWhite];
    [btn_viewWhite addTarget:self action:@selector(btn_viewWhiteAction:) forControlEvents:(UIControlEventTouchUpInside)];
    
    //
    UIView * view_WhiteMoney = [[UIView alloc] initWithFrame:CGRectMake(0, CGRectGetMaxY(view_White.frame) + 15, SCREEN_WIDTH, 50)];
    view_WhiteMoney.backgroundColor = [UIColor whiteColor];
    [self.scrollView addSubview:view_WhiteMoney];
    
    UILabel * lbl_Money = [[UILabel alloc] initWithFrame:CGRectMake(20, 15, 70, 20)];
    lbl_Money.text = @"金额(元)";
    [view_WhiteMoney addSubview:lbl_Money];
    
    self.text_Money = [[UITextField alloc] initWithFrame:CGRectMake(CGRectGetMaxX(lbl_Money.frame) + 20, 12.5, SCREEN_WIDTH - CGRectGetMaxX(lbl_Money.frame) - 20 - 15, 25)];
    self.text_Money.placeholder = @"请输入金额";
    self.text_Money.delegate = self;
    self.text_Money.keyboardType = UIKeyboardTypeDecimalPad;
    [view_WhiteMoney addSubview:self.text_Money];
    [self.text_Money addTarget:self action:@selector(text_MoneyAction:) forControlEvents:(UIControlEventEditingChanged)];
    
    //
    self.btn_Next = [UIButton buttonWithType:(UIButtonTypeCustom)];
    self.btn_Next.frame = CGRectMake(15, CGRectGetMaxY(view_WhiteMoney.frame) + 40, SCREEN_WIDTH - 30, 45);
    self.btn_Next.layer.cornerRadius = 5;
    self.btn_Next.backgroundColor = [UIColor colorWithRed:226/255.0 green:227/255.0 blue:228/255.0 alpha:1.0f];
    [self.btn_Next setTitle:@"下一步" forState:(UIControlStateNormal)];
    self.btn_Next.userInteractionEnabled = NO;
    [self.btn_Next setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    [self.scrollView addSubview:self.btn_Next];
    [self.btn_Next addTarget:self action:@selector(btn_NextAction:) forControlEvents:(UIControlEventTouchUpInside)];
}

#pragma mark - scrollView代理
- (void )scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    [self.text_Money resignFirstResponder];
}

#pragma mark - textfield代理
- (void )text_MoneyAction:(UITextField *)TextField
{
    if([self.text_Money.text length] == 0)
    {
        self.btn_Next.userInteractionEnabled = NO;
        self.btn_Next.backgroundColor = [UIColor colorWithRed:226/255.0 green:227/255.0 blue:228/255.0 alpha:1.0f];
        [self.btn_Next setTitleColor:[UIColor grayColor] forState:(UIControlStateNormal)];
    }
    else
    {
        self.btn_Next.userInteractionEnabled = YES;
        self.btn_Next.backgroundColor = [UIColor orangeColor];
        [self.btn_Next setTitleColor:[UIColor whiteColor] forState:(UIControlStateNormal)];
    }
}

#pragma mark - 点击下一步
- (void)btn_NextAction:(UIButton *)sender
{
    NSLog(@"点击下一步,开启验证");
    
    [self.text_Money resignFirstResponder];
}

#pragma mark - 点击换充值方式
- (void )btn_viewWhiteAction:(UIButton *)sender
{
    [self.text_Money resignFirstResponder];
    
    UIAlertController * alert = [UIAlertController alertControllerWithTitle:nil message:@"请选择支付方式" preferredStyle:(UIAlertControllerStyleActionSheet)];
    [self presentViewController:alert animated:YES completion:^{
    }];
    
    UIAlertAction * action_Weixin = [UIAlertAction actionWithTitle:@"微信支付" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        self.image_Pic.image = [UIImage imageNamed:@"Wchat.png"];
        self.name.text = @"微信支付";
        self.detail.text = @"使用微信安全支付";
        
    }];
    [alert addAction:action_Weixin];
    
    
    UIAlertAction * action_Card = [UIAlertAction actionWithTitle:@"使用新卡充值" style:(UIAlertActionStyleDefault) handler:^(UIAlertAction * _Nonnull action) {
        
        NSUserDefaults * userdefault = [NSUserDefaults standardUserDefaults];
    
        if(![[userdefault objectForKey:@"Card_Number"] isEqualToString:@"1"])
        {
            //如果银行卡数量为空
            Add_RechargeCardViewController * add_RechargeCardViewController = [[Add_RechargeCardViewController alloc] init];
            [self showViewController:add_RechargeCardViewController sender:nil];
        }
        else
        {
            self.image_Pic.image = [UIImage imageNamed:@"New_Card.png"];
            self.name.text = @"新卡支付";
            self.detail.text = @"使用新卡安全支付";
        }
        
    }];
    [alert addAction:action_Card];
    
    UIAlertAction * action_Cancel = [UIAlertAction actionWithTitle:@"取消" style:(UIAlertActionStyleCancel) handler:^(UIAlertAction * _Nonnull action) {
        
    }];
    [alert addAction:action_Cancel];
}














@end
